//
//  YWProgressView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/9/25.
//

#import "YWProgressView.h"

@interface YWProgressView ()


@end

@implementation YWProgressView

-(void)awakeFromNib{
    [super awakeFromNib];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tap];
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self addGestureRecognizer:longPress];
    longPress.minimumPressDuration = 0.6;
    
}

-(void)tap{
    
    if([self.delegate respondsToSelector:@selector(progressViewDidTap:)]){
        [self.delegate progressViewDidTap:self];
    }
}

-(void)longPress:(UIPanGestureRecognizer *)panGesture{
    
    //判断手势状态
    switch (panGesture.state) {
            
            //一开始拖拽
        case UIGestureRecognizerStateBegan:{
            if([self.delegate respondsToSelector:@selector(progressViewDidStartLongPress:)]){
                [self.delegate progressViewDidStartLongPress:self];
            }
          
            
        }
            break;
        case UIGestureRecognizerStateChanged:{
            
            
        }
            break;
        case UIGestureRecognizerStateEnded:{
            if([self.delegate respondsToSelector:@selector(progressViewDidEndLongPress:)]){
                [self.delegate progressViewDidEndLongPress:self];
            }
         
            
        }
            break;
        default:
            
            break;
    }
    
}

-(void)setProgressValue:(CGFloat)progressValue{
    _progressValue = progressValue;
    
    [self setNeedsDisplay];
}

- (void)clearProgress{
    
    self.progressValue = 0;
    
    [self setNeedsDisplay];
    
}

-(void)drawRect:(CGRect)rect{
    
    CGFloat viewWidth = rect.size.width;
    CGFloat viewHeight = rect.size.height;
    
    [[UIColor redColor] set]; //设置描边颜色
    
    CGFloat x = 10;
    CGFloat y = 10;
    CGFloat width = viewWidth - 2*x;
    CGFloat height = viewHeight - 2*y;
    CGFloat radius = width * 0.5;
    
    //cornerRadius:圆角半径
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, width, height) cornerRadius:radius];
    [path1 fill];
    
    
    CGPoint center = CGPointMake(viewWidth/2.0, viewHeight/2.0);  //设置圆心位置
    CGFloat radius2 = viewHeight/2.0-5;  //设置半径
    CGFloat startA = - M_PI_2;  //圆起点位置
    CGFloat endA = -M_PI_2 + M_PI * 2 * self.progressValue;  //圆终点位置
    
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius2 startAngle:startA endAngle:endA clockwise:YES];
    path2.lineWidth = 5;
    [path2 stroke];
    
    
}


@end
